package com.wnxy.exception;


import com.wnxy.dto.Result;
import com.wnxy.dto.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalException {

    @ExceptionHandler(RootException.class)
    public Result findExceptionHandler(RootException e) {
        String message = "请求失败:";
        if (null != e.getMessage()) {
            message = e.getMessage();
        }
        log.error(message, e);
        return Result.getInstance()
                .setFlag(false).setStatusCode(e.getCode())
                .setMessage(message);
    }
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e){
      log.error("异常:",e);
      return Result.getInstance()
              .setFlag(false).setStatusCode(StatusCode.ERROR.getCode())
              .setMessage("服务器异常");
    }
    @ExceptionHandler(AccessDeniedException.class)
    public Result accessDeniedExceptionHandler(AccessDeniedException e){
        log.error("异常:",e);
        return Result.getInstance()
                .setFlag(false).setStatusCode(403)
                .setMessage("服务器异常");
    }
}